/**
 *
 */
requirejs.config({
    paths : {
        "header" : "app/head/header",
        "labs" : "app/labs/labs",
        "footer" : "app/footer/footer",
        "lab" : "app/lab/lab",
        'notice' : 'app/lab/notice/notice',
        'document' : 'app/lab/document/document',
        'order' : 'app/lab/order/order',
        'inventory' : 'app/lab/inventory/inventory',
        'share' : 'app/lab/shareEquipment/shareEquipment'
    }
});

define(['history','header','footer','labs','lab','notice','document','order','inventory','share'],function(_history,header,footer,labs,lab,notice,doc,order,inventory,share){
   var router = {
       map : {
           "labs" : labs,
           "lab" : lab,
           "notice" : notice,
           "document" : doc,
           "order" : order,
           "inventory" : inventory,
           "share" : share
       }
   };

   router.init = function(map){
       if(map) {
           router.map = map;
       }
       router.checkUrl();
       window.addEventListener("popstate",function(state){
           console.log(state.state);
           router.checkUrl();
       });
   };

    router.checkUrl = function(){
        var query = _history.getHash();
        if(query) {
            router.loadUrl(query);
        } else {
            console.log("没有");
            _history.replace("labs","labs");
            router.checkUrl();
        }
    };

    router.loadUrl = function(url) {
        router.handle(url);
    };

   router.handle = function(url) {
       //spliter url
       if(url) {
           var arrays = router.separete(url);
           router.load(arrays);
       }
   };

   router.separete = function(url){
        if(typeof(url) === 'string') {
            return url.split('/');
        } else {
            console.log("error!url is not string");
            return [];
        }
   };

   router.load = function(arrays) {
       if(!!arrays[0]) {
           router.loadMain(arrays[0],arrays)
       }
   };

   router.loadDefault = function() {
       if(!!$('header')) {
           $('body').empty()
               .css('background-image','url("image/background.png")')
               .css('background-repeat','repeat');
           header.init();
           footer.init();
       }
   };

   router.loadMain = function(value,arrays) {
       router.loadDefault();
       if(!!$('header')){
           if(router.map[value]){
               if(arrays[1]) {
                   router.map[value].init(arrays[1]);
                   if(arrays[2]) {
                       router.map[value].trigger(arrays[2]);
                   }
               } else {
                   router.map[value].init();
               }
           } else {
               console.log("unknow");
           }
       }
   };

   return router;
});
